<?php
class News extends DataObject {
	static $db = array (
		"Title_en" => "Varchar(255)",
		"Summary_en"=>"Varchar(2000)",
		"Content_en"=>"HTMLText", 
		"ImageAlt_en"=>"Varchar(255)",

		"Title_vi" => "Varchar(255)",
		"Summary_vi"=>"Varchar(2000)",
		"Content_vi"=>"HTMLText", 
		"ImageAlt_vi"=>"Varchar(255)",
		"PublishDate"=>"Datetime",
		"Status"=>"Boolean",
		"Focus"=>"Boolean",   
	);
	static $has_one = array (
		'Image'=>'Image',
		'Category'=>'NewsCategory',
	);

	static $searchable_fields = array (
		'CategoryID'=> array('title'=>'Category'),
		'Title_en'=> array('title'=>'Title'),
		'Status'=> array('title'=>'IsShow?'),
	);
	/**
	 * Overload so that the default sort.
	 */
	static $default_sort = 'PublishDate DESC';

	static $summary_fields = array (
		'Thumbnail'=>"Image",
		'Title_en' => 'Title',
		'PublishDateFomat' => 'Publish Date',
		'StatusName' => 'Is Show',
	);
	function getStatusName()
	{
		if ($this->Status)return 'Yes';
		else return 'No';
	}
	function getPublishDateFomat()
	{
		$date = new DateTime($this->PublishDate);
		return date_format($date, 'd/m/Y');

	}
	function getThumbnail()
	{
		if ($Image = $this->Image())
		{
			return $Image->CMSThumbnail();
		}
		else
		{
			return '(No Image)';
		}
	}
	static $defaults = array(
		'Status' => 'True',

	);
	/**
	 * Create field for CMS
	 */
	function getCMSFields($controller = null) {
		$drpCategoy=Lib::dropdown("NewsCategory",null,"Name_en","CategoryID","Category Name",false);
		$publishDate = new DateField('PublishDate',_t('Article.PUBLISHDATE', 'Ngày đăng'),getdate());
		$publishDate->setConfig('showcalendar', true);
		$publishDate->setConfig('dateformat', 'dd/MM/YYYY');
		$publishDate->setValue(strtotime('now'));
		$ArticlePub= new Tab(_t('Article.TABCONTENTTITLEVN', 'Image'),
		$publishDate,
		new CheckboxField("Status", _t('Article.STATUS', 'Is Show')),
		//new CheckboxField("Focus", _t('Article.STATUS', 'Hiển thị ở trang chủ')),
		new ImageField("Image", _t('Article.IMAGE', 'Image'))
		);
		$ArticleTabVn= new Tab(_t('Article.TABCONTENTTITLEVN', 'Tiếng Việt'),
		new TextField("Title_vi", _t('Article.TITLE', 'Tiêu đề')),
		new TextareaField("Summary_vi", _t('Article.SUMMARY', 'Tóm tắt'),3,20),
		new HtmlEditorField("Content_vi", _t('Article.CONTENT', 'Nội dung'))
		);
		$ArticleTabEn= new Tab(_t('Article.TABCONTENTTITLEVN', 'News'),
		$drpCategoy,
		new TextField("Title_en", _t('Article.TITLE', 'Title')),
		new TextareaField("Summary_en", _t('Article.SUMMARY', 'Summary'),3,20),
		new HtmlEditorField("Content_en", _t('Article.CONTENT', 'Content'))
		);
		if($this->ID)
		$fields = new FieldSet(new TabSet("Root",$ArticleTabEn,$ArticlePub));
		else
		$fields = new FieldSet(new TabSet("Root",$ArticleTabEn));
		return $fields;
	}
	public function scaffoldSearchFields()
	{
		$fields = parent::scaffoldSearchFields();
		$drpCategoy=Lib::dropdown("NewsCategory",null,"Name_en","CategoryID","Category Name",true);
		$drpStatus= Lib::dropdownYesNo('Status','Is Show');
		$fields->replaceField('Status', $drpStatus);
		return $fields;
	}

	function getCMSValidator() {
		return new RequiredFields(array('Title_vi','PublishDate'));
	}
	function canCreate() {return true;}
	function canEdit() {return true;}
	function canDelete() {return true;}
}
?>